package com.heyi.saas.start.config;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.heyi.saas.start.constant.SaasConstant;
import com.heyi.saas.start.service.TenantContext;
import com.heyi.saas.start.service.TenantTables;
import com.heyi.saas.start.util.oConvertUtils;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author dengfeng
 * @Description:
 * @date 2021/9/2214:53
 */
public class MyTenantLineHandler implements TenantLineHandler {

    public TenantTables tenantTables;

    public MyTenantLineHandler(TenantTables tenantTables){
        this.tenantTables = tenantTables;
    }

    /**
    　* 获取租户 ID 值表达式，只支持单个 ID 值
    　* @author dengfeng
    　* @date 2021/9/23 9:08
    　*/
    @Override
    public Expression getTenantId() {
        String tenant_id = oConvertUtils.getString(TenantContext.getTenant(), "0");
        return new LongValue(tenant_id);
    }

    /**
     　* 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件,
     　* @author dengfeng
     　* @date 2021/9/23 9:08
     　*/
    @Override
    public boolean ignoreTable(String tableName) {

        return !tenantTables.getTenantTableList().contains(tableName);
    }

    @Override
    public String getTenantIdColumn() {
        return SaasConstant.TENANT_FIELD;
    }
}
